package ro.tm.cstaicu.cosmote;

import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.RemoteViews;

public class SMSReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {

		Bundle bundle = intent.getExtras();
		SmsMessage[] msgs = null;
		if (bundle != null) {
			// ---retrieve the SMS message received---
			Object[] pdus = (Object[]) bundle.get("pdus");
			msgs = new SmsMessage[pdus.length];
			for (int i = 0; i < 1; i++) {
				msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
				AppWidgetManager manager = AppWidgetManager
						.getInstance(context);

				AppWidgetManager appWidgetManager = AppWidgetManager
						.getInstance(context);
				int[] appWidgetIds = appWidgetManager
						.getAppWidgetIds(new ComponentName(context,
								CosmoteWidgetProvider.class));
				RemoteViews views = new RemoteViews(context.getPackageName(),
						R.layout.layout_widget);
				for (int widgetID : appWidgetIds) {
					
					views.setTextViewText(
							R.id.textView1,
							msgs[i].getMessageBody().toString());
					manager.updateAppWidget(widgetID, views);
				}
				 Log.d("ro.tm.cstaicu", msgs[i].getOriginatingAddress() + " "
				 + msgs[i].getMessageBody().toString());

			}
		}
	}

}
